import { fileURLToPath, URL } from 'node:url';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import path from 'node:path';

const baseUrl = 'http://123.233.115.107:18081'; // 后端接口
const mockUrl = 'http://127.0.0.1:4523/m1/7003387-6721897-default'; // mock接口

// https://vite.dev/config/
export default defineConfig({
  base: './',
  plugins: [
    vue(),
    AutoImport({
      imports: ['vue', 'vue-router', 'pinia'],
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
    createSvgIconsPlugin({
      // Specify the icon folder to be cached
      iconDirs: [path.resolve(process.cwd(), 'src/assets/icons-svg')],
      // Specify symbolId format
      symbolId: 'icon-[dir]-[name]',
      svgoOptions: {
        plugins: [
          'preset-default', // built-in plugins enabled by default
          'prefixIds', // enable built-in plugins by name
          'removeDimensions', //移除width,height
          // enable built-in plugins with an object to configure plugins
          {
            name: 'convertColors',
            params: {
              currentColor: true, //颜色
            },
          },
        ],
      },
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  css: {
    devSourcemap: true,
    preprocessorOptions: {
      scss: {
        additionalData: `@use '@/assets/css/utils.scss' as *;`,
      },
    },
  },
  server: {
    proxy: {
      '/ws': {
        target: baseUrl,
        ws: true,
        changeOrigin: true,
        rewrite: (p) => p.replace(/^\/ws/, ''),
      },
      '/api/mock': {
        target: mockUrl,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/mock/, ''),
      },
      '/api': {
        target: baseUrl,
        changeOrigin: true,
        rewrite: (p) => p.replace(/^\/api/, ''),
      },
    },
  },
});
